var ts = {
  
    upload_url: 'https://zhapp.4hl.cn/app/user/App_UserHead',//API接口
    img_head: 'https://zhapp.4hl.cn/',
    select_img: function(el, cb) {
        document.querySelector("#" + el).addEventListener("click", function() {
            plus.gallery.pick(function(img_path) {
//				console.log("选择照片完成")
                ts.uploadHead(img_path, function(path) {
                    cb(path)
                });
            }, function(err) {
                console.log('err')
            });
        })
    },
    get_img: function(el, cb) {
        document.querySelector("#" + el).addEventListener("click", function() {
            // 拍照
            var cmr = plus.camera.getCamera();
            var res = cmr.supportedImageResolutions[0];
            var fmt = cmr.supportedImageFormats[0];
            cmr.captureImage(function(path) {
//				console.log("拍照成功: " + path);
                plus.io.resolveLocalFileSystemURL(path, function(path2) {
                        var new_path = path2.toLocalURL();
                        ts.uploadHead(new_path, function(img_path) {
                            cb(img_path)
                        })
                    },
                    function(error) {
                        alert("拍照失败: " + error.message);
                    }, {
                        resolution: res,
                        format: fmt
                    }
                );
            })
        })
    },
    uploadHead: function(imgPath, callback) {
        //console.log("进入上传图片方法,接收到图片路径为" + imgPath)
        var image = new Image();
        image.src = imgPath;
        var ex = imgPath.slice(-3)
        //console.log(ex)
        image.onload = function() {
            var imgData = ts.getBase64Image(image);
 
//			console.log("图片base64长度：" + imgData.length);
            mui.ajax(ts.upload_url, {
                data: {
                    imgData: imgData,
                },
                dataType: 'json', //服务器返回json格式数据
                type: 'post', //HTTP请求类型
                timeout: 10000, //超时时间设置为10秒；
 
                success: function(data) {
                  
 					callback(ts.img_head+data)
                },
                error: function(XMLHttpRequest,xhr, type, errorThrown) {

                }
            });
//          mui.post(ts.upload_url, {
//              imgData: imgData
//          }, function(data) {
//              callback(ts.img_head + data)
//              console.log(data);
//              if(data.code == 200) {
//                  callback(ts.img_head + data)
//              }
//          });
        }
    },
    getBase64Image: function(img) {
        var canvas = document.createElement("canvas");
        var width = img.width;
        //console.log(width);
        var height = img.height;
        //console.log(height);

        // calculate the width and height, constraining the proportions
        if(width > height) {
            if(width > 480) {
                height = Math.round(height *= 480 / width);

                width = 480;
            }
        } else {
            if(height > 480) {
                width = Math.round(width *= 480 / height);
                height = 480;
            }
        }
        canvas.width = width; /*设置新的图片的宽度*/

        canvas.height = height; /*设置新的图片的长度*/

        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, width, height); /*绘图*/
        var dataURL = canvas.toDataURL("image/jpeg", 0.8);
        //	console.log(dataURL)
        return dataURL.replace("data:image/jpeg;base64,", "");
    }
}